import {
  getRoles,
  addRoles,
  updRoles,
  delRoles,
  getDeptList,
  getDeptIds,
  getAllMenuTree,
  getRoleMenuTree,
  updRoleMenu,
} from '../services/api';

export default {
  namespace: 'roles',

  state: {
    roles: {
      list: [],
      pagination: {},
    },
    deptList: [],
    allMenuTree: [],
    roleMenuTree: [],
  },

  effects: {
    * fetch({payload}, {call, put}) {
      const response = yield call(getRoles, payload);
      console.log(response);
      if (response) {
        const {records, total, size, current} = response;
        const roles = {
          list: records,
          pagination: {
            total,
            pageSize: size,
            current,
          },
        };
        yield put({
          type: 'save',
          roles,
        });
      }
    },
    * add({payload}, {select, call, put}) {
      const response = yield call(addRoles, payload);

      if (response && response.code === 0) {

        const pagination = yield select(state => state.laborPerson.data.pagination);
        const params = {
          page: pagination.current,
          limit: pagination.pageSize,
        };
        const data = yield call(queryPerson, params);
        if (data) {
          const {records, total, size, current} = data;
          const roles = {
            list: records,
            pagination: {
              total,
              pageSize: size,
              current,
            },
          };
          yield put({
            type: 'save',
            roles,
          });
        }
      }
    },
    * del({payload}, {call, put}) {
      const response = yield call(delRoles, payload);
      console.log(response);
      if (response) {
        yield put({
          type: 'fetch',
        });
      }
    },
    * upd({payload}, {call, put}) {
      const response = yield call(updRoles, payload);
      console.log(response);
      if (response) {
        yield put({
          type: 'fetch',
        });
      }
    },
    * fetchDeptList({payload}, {call, put}) {
      const response = yield call(getDeptList, payload);
      if (response) {
        yield put({
          type: 'saveDeptList',
          deptList: response,
        });
      }
    },
    * fetchDeptIds({payload}, {call, put}) {
      const response = yield call(getDeptIds, payload);
      console.log(response);
      if (response) {
        yield put({
          type: 'saveDeptIds',
          deptIds: response,
        });
      }
    },
    * initDept({payload}, {call, put}) {
      const deptList = yield call(getDeptList, payload);
      const deptIds = yield call(getDeptIds, payload);
      if (deptList && deptIds) {
        yield put({
          type: 'saveDept',
          deptList,
          deptIds,
        });
      }
    },
    * initMenuTree({payload}, {call, put}) {
      const allMenuTree = yield call(getAllMenuTree, payload);
      const roleMenuTree = yield call(getRoleMenuTree, payload);
      if (allMenuTree && roleMenuTree) {
        yield put({
          type: 'saveMenuTree',
          allMenuTree,
          roleMenuTree,
        });
      }
    },
    * updRoleMenu({payload}, {call, put}) {
      const response = yield call(updRoleMenu, payload);
      console.log(response);
      if (response) {
        yield put({
          type: 'fetch',
        });
      }
    },
  },

  reducers: {
    save(state, {roles}) {
      return {
        ...state,
        roles,
      };
    },
    saveDept(state, {deptList, deptIds}) {
      return {
        ...state,
        deptList,
        deptIds,
      };
    },
    saveDeptList(state, {deptList}) {
      return {
        ...state,
        deptList,
      };
    },
    saveDeptIds(state, {deptIds}) {
      return {
        ...state,
        deptIds,
      };
    },
    saveMenuTree(state, {allMenuTree, roleMenuTree}) {
      return {
        ...state,
        allMenuTree,
        roleMenuTree,
      };
    },
  },
};
